﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;


namespace PlanningDepartment
{
    public partial class FrDoiMatKhau : Telerik.WinControls.UI.RadForm
    {
        public FrDoiMatKhau()
        {
            InitializeComponent();
        }
        private void ChangeThemeName(Control control, string themeName)
        {
            IComponentTreeHandler radControl = control as IComponentTreeHandler;
            if (radControl != null) { radControl.ThemeName = themeName; }
            foreach (Control child in control.Controls)
            { ChangeThemeName(child, themeName); }
        }
        private string Openfile_Image()
        {
            string path = null;
            OpenFileDialog dlg = new OpenFileDialog();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes != DialogResult.Cancel)
            {
                //Set image in picture box
                pictureBox1.ImageLocation = dlg.FileName;

                //Provide file path in txtImagePath text box.
                path = dlg.FileName;
                return path;
            }
            return null;
        }
       
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            lbImage.Text = Openfile_Image();
        }
        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
        private void bt_DangNhap_Click(object sender, EventArgs e)
        {
            Process dta=new Process();
            string sql_select = @"select UsersName,Password from Users";
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd1 = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.CommandText = sql_select;
                cmd.Connection = Conn;
                cmd1.Connection = Conn;
                SqlDataReader rd = cmd.ExecuteReader();
                cmd1.CommandType = CommandType.StoredProcedure;
                int check_ = 0;
                
                while (rd.Read())
                {
                    if (txt_UserName.Text == rd[0].ToString() && txt_Passold.Text == rd[1].ToString())
                    {
                        
                        byte[] imageData = null;
                        //MessageBox.Show("aaaa");
                        //Read Image Bytes into a byte array
                        if (lbImage.Text != "Image")
                            imageData = ReadFile(lbImage.Text);
                       // MessageBox.Show("bb");
                        if (imageData != null)
                        {
                            if (imageData.Length > 50000)
                                MessageBox.Show("Bạn nên giảm dung lượng ảnh lại trước khi lưu ảnh");
                            else
                            {
                                
                                cmd1.CommandText = "Upd_Users";
                                cmd1.Parameters.AddWithValue("@Images", (object)imageData);
                                cmd1.Parameters.AddWithValue("@UsersName", txt_UserName.Text);
                                cmd1.Parameters.AddWithValue("@Password", txt_Passnew.Text);
                                check_ = 1;
                            }
                        }
                        else
                        {
                           
                            cmd1.CommandText = "Upd_Users1";
                           
                            cmd1.Parameters.AddWithValue("@UsersName", txt_UserName.Text);
                            cmd1.Parameters.AddWithValue("@Password", txt_Passnew.Text);
                            check_ = 1;
                        }
                       
                        
                        break;
                    }
                   
                }
                rd.Close();
                if (check_ == 1)
                {
                   
                    cmd1.ExecuteNonQuery();
                    MessageBox.Show("Thay đổi Password thành công");
                    txt_Passnew.Text = "";
                    txt_UserName.Text = "";
                    txt_Passold.Text = "";
                }
                
                else
                    MessageBox.Show("Tên hoặc Mật Mã chưa đúng");
                Conn.Close();
                cmd.Dispose();
                cmd1.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        
        }

        
       

       
    }
}
